﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
using System.Collections.Generic;
namespace WPFExcelReport
{
    //ICC_ABAX;ABPOS;ABAUFM;GAAUF;ABFOLGE;
    public partial class YGRABDOCData : BaseFunction
    {
        public Dictionary<string, object> ENVMAIN
        {
            get
            {
                return app.ENVMAIN(app.UserIP);
            }
        }
        VGAAUF _GD;
        public VGAAUF GD
        {
            get
            {
                if (_GD == null) _GD = new VGAAUF();
                return _GD;
            }
        }
        VZUTEI _ZU;
        public VZUTEI ZU
        {
            get
            {
                if (_ZU == null) _ZU = new VZUTEI();
                return _ZU;
            }
        }
        VABPOS _ABP;
        public VABPOS ABP
        {
            get
            {
                if (_ABP == null) _ABP = new VABPOS();
                return _ABP;
            }
        }
        public VABAUFM VMU
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VMU"))
                    ENVMAIN["VMU"] = new VABAUFM();
                return (VABAUFM)ENVMAIN["VMU"];
            }
        }
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        SETINTEXDB INTEXDB = new SETINTEXDB();
        public YGRABDOCData()
            : base()
        {
            Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["SDSA"] = "0015";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0209";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0014";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            return SReturn;
        }
        public DataSet LoadMUReq(string client, string KRSs)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["GAMANDANT"] = client;
            dwData["C2"] = Helper.Left(KRSs.PadRight(3), 3);
            SResult = INTEXDBC.QueryGAAUF(dwData, 11);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public SETINTEXDB BuildData(string client, string KRS, string LFD)
        {
            DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB();

            SReturn.Merge(LoadEnv());
            SResult = ABP.QueryABK(MANDANT, KRS, LFD);
            if (SResult.Tables.Contains("ABPOS"))
            {
                foreach (DataRow row in SResult.Tables["ABPOS"].Rows)
                {
                    row["ABPART"] = Helper.Convert8859P1ToGB2312(row["ABPART"].ToString());
                }
            }
            SReturn.Merge(SResult);

            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["ABAMANDANT"] = MANDANT;
            dwData["ABAAUFKRS"] = KRS;
            dwData["ABAAUFLFD"] = LFD;

            SResult =VMU.QMU10(dwData);
            SResult.Tables[0].TableName = "TBABA";
            SReturn.Merge(SResult);
            string processed = "";
            DataSet SetTmp = new DataSet();
            foreach (DataRow row in SReturn.Tables["TBABA"].Rows)
            {
                if (row["ABAVKAUFTYP"].ToString() == "K")
                {
                    string ABAVKAUFKRS = row["ABAVKAUFKRS"].ToString();
                    string ABAVKAUFLFD = row["ABAVKAUFLFD"].ToString();
                    if (processed.Contains(ABAVKAUFKRS + ABAVKAUFLFD + ";")) continue;
                    dwData["VKPMANDANT"] = MANDANT;
                    dwData["VKPAUFKRS"] = ABAVKAUFKRS;
                    dwData["VKPAUFLFD"] = ABAVKAUFLFD;
                    SetTmp.Merge(INTEXDBC.QueryVKPOS(dwData, 3));
                    processed += ABAVKAUFKRS + ABAVKAUFLFD + ";";
                }
            }
            SReturn.Merge(SetTmp);

            return SReturn;
        }

    }
}